package com.hotcars.action;

import java.util.List;

import org.apache.struts2.ServletActionContext;

import com.hotcars.dao.CarDao;
import com.hotcars.model.Car;

public class CarAction {

	private List<Car> cars;
	private Car car;
	private CarDao dao;
	
	public CarAction() {
		dao = new CarDao(); 
	}

	/**
	 * @return the cars
	 */
	public List<Car> getCars() {
		return cars;
	}

	/**
	 * @param cars the cars to set
	 */
	public void setCars(List<Car> cars) {
		this.cars = cars;
	}

	/**
	 * @return the car
	 */
	public Car getCar() {
		return car;
	}

	/**
	 * @param car the car to set
	 */
	public void setCar(Car car) {
		this.car = car;
	}

	/* List method to get all cars */
	public String listAll() {
		this.cars = dao.getCars();
		return "SUCCESS";
	}
	
	/* List method to get a car by id */
	public String listCar() {
		if((ServletActionContext.getRequest().getParameter("id") != null) && ("" != ServletActionContext.getRequest().getParameter("id"))) 
			this.car = dao.getCar(Integer.parseInt(ServletActionContext.getRequest().getParameter("id")));
		return "SUCCESS";
	}
	
}